package com.example.wowangz.modules.user.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.wowangz.modules.user.model.User;
import com.example.wowangz.modules.user.service.UserService;
import com.example.wowangz.rabbitmq.send.QueueSenderService;

/**
 * User的控制层
 * 
 */
@RestController
@RequestMapping("/user")
public class UserController {

	private final Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private UserService userService;

	@Autowired
	@Qualifier("redisTemplate")
	private RedisTemplate<String, String> redisTemplate;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private QueueSenderService queueSenderService;

	@RequestMapping(value = "/users")
	public String selectUsers() {
		log.debug("debug");
		log.info("info");
		log.warn("warn");
		log.error("error");
		List<User> userList = userService.selectUsers();
		if (null == userList || userList.isEmpty()) {
			return null;
		}
		queueSenderService.sendMsgToQueueOne("hello rabbitmq!");
		return userList.toString();
	}

	@RequestMapping(value = "/users/{id}")
	public String userDetail(@PathVariable String id) {
		User user = userService.getUserById(id);
		queueSenderService.sendMsgToQueueTwo(user);
		return user.toString();
	}

	@RequestMapping(value = "/condition")
	public String select(User user) {
		List<User> userList = userService.getUserByCondition(user);
		if (null == userList || userList.isEmpty()) {
			return null;
		}
		return userList.toString();
	}

	@RequestMapping(value = "/redis")
	public String redis() {
		// 保存字符串
		redisTemplate.opsForValue().set("aaa", "111");
		String str1 = redisTemplate.opsForValue().get("aaa");

		stringRedisTemplate.opsForValue().set("bbb", "222");
		String str2 = stringRedisTemplate.opsForValue().get("bbb");
		return str1 + str2;
	}

}
